<script setup lang="ts">
import { ref } from "vue";
import { useAd } from "../utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import Upload from "@iconify-icons/ep/upload";
import Download from "@iconify-icons/ep/download";
import View from "@iconify-icons/ep/view";
import VideoPlay from "@iconify-icons/ep/video-play";
import VideoPause from "@iconify-icons/ep/video-pause";

defineOptions({
  name: "AdList"
});

const formRef = ref();
const tableRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  positionList,
  pagination,
  buttonClass,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleBatchDelete,
  handlePublish,
  handleUnpublish,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useAd();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="广告名称：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入广告名称"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="广告位：" prop="positionId">
        <el-select
          v-model="form.positionId"
          placeholder="请选择广告位"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="position in positionList"
            :key="position.id"
            :label="position.name"
            :value="position.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="广告类型：" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择广告类型"
          clearable
          class="!w-[180px]"
        >
          <el-option label="图片广告" :value="1" />
          <el-option label="文字广告" :value="2" />
          <el-option label="视频广告" :value="3" />
          <el-option label="轮播广告" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          clearable
          class="!w-[180px]"
        >
          <el-option label="草稿" :value="0" />
          <el-option label="投放中" :value="1" />
          <el-option label="已暂停" :value="2" />
          <el-option label="已结束" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="广告主：" prop="advertiser">
        <el-input
          v-model="form.advertiser"
          placeholder="请输入广告主"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="创建时间：" prop="createTime">
        <el-date-picker
          v-model="form.createTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          class="!w-[240px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="广告列表" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新增广告
        </el-button>
        <el-button
          type="danger"
          :icon="useRenderIcon(Delete)"
          @click="handleBatchDelete($refs.tableRef?.getSelectionRows())"
        >
          批量删除
        </el-button>
        <el-button
          type="warning"
          :icon="useRenderIcon(Upload)"
          @click="() => {}"
        >
          导入数据
        </el-button>
        <el-button
          type="success"
          :icon="useRenderIcon(Download)"
          @click="() => {}"
        >
          导出数据
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="openDialog('查看', row)"
            >
              查看
            </el-button>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('编辑', row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              :title="`是否确认删除广告名称为${row.name}的这条数据`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
            <el-button
              v-if="row.status === 0 || row.status === 2"
              class="reset-margin"
              link
              type="success"
              :size="size"
              :icon="useRenderIcon(VideoPlay)"
              @click="handlePublish(row)"
            >
              上线
            </el-button>
            <el-button
              v-if="row.status === 1"
              class="reset-margin"
              link
              type="warning"
              :size="size"
              :icon="useRenderIcon(VideoPause)"
              @click="handleUnpublish(row)"
            >
              下线
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>